package org.sgroup.imenu.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.w3c.dom.Document;
import org.xmlpull.v1.XmlPullParserException;

public class WebserviceHelper {

	
 	public static Object InvokeWebmethod(String Url,String methodName,String namespace,String[] paras,Object[] values )
 	{
 		String SOAP_ACTION = namespace  + methodName;
 		String METHOD_NAME = methodName;
 		SoapObject Request=new SoapObject(namespace, METHOD_NAME);	
 		//Add parameter
 		if(paras !=null)
 		{
 			if(paras.length != values.length)
 			{
 				return "";
 			}
 			for(int i=0;i<paras.length;i++)
 			{
 				Request.addProperty(paras[i], values[i]);
 			}
 		}
		SoapSerializationEnvelope envelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet=true;
		envelope.setOutputSoapObject(Request);
		
		AndroidHttpTransport aht=new AndroidHttpTransport(Url);
		try {
			aht.call(SOAP_ACTION, envelope);
			Object obj = envelope.getResponse();
			//SoapPrimitive result=(SoapPrimitive)envelope.getResponse();
			return obj;
		} catch (IOException e) {
			//String s=e.getMessage();
			e.printStackTrace();
			//LogManager.WriteErrorLog("InvokeWebmethod", s);
		} catch (XmlPullParserException e) {
			//String d=e.getMessage();
			e.printStackTrace();
			//LogManager.WriteErrorLog("InvokeWebmethod", d);
		}
		
 		return null;
 	}
 	
 	public static String InvokeMethodByHTTPClient(String Url)
 	{
 		
 		BufferedReader in = null;
 		try {
	 		HttpClient client = new DefaultHttpClient();
	 		HttpGet request = new
	 		HttpGet("http://code.google.com/android/");
	 		HttpResponse response = client.execute(request);
	 		in = new BufferedReader(
	 		new InputStreamReader(
	 		response.getEntity().getContent()));
	 		StringBuffer sb = new StringBuffer("");
	 		String line = "";
	 		String NL = System.getProperty("line.separator");
	 		while ((line = in.readLine()) != null) {
	 			sb.append(line + NL);
	 		}
	 		in.close();
	 		String page = sb.toString();
	 		
	  	    return page;
 		}
 		catch(Exception e)
 		{
 			e.printStackTrace();
 			return "";
 		}
 		
 	}
}
